package com.service.impl;
import java.util.List;
import javax.annotation.Resource;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.entity.Article;
import com.entity.Category;
import com.entity.Tag;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.ArticleMapper;
import com.mapper.CategoryMapper;
import com.service.ArticleService;

@Service
public class ArticleServiceImpl implements ArticleService{
	
	@Resource
	private ArticleMapper articleMapper;
	
	@Resource
	private CategoryMapper categoryMapper;
	
	public List<Article> listRecentArticle(Integer n) {
		return articleMapper.listRecentArticle(n);
	}

//	1.缓存中没有数据时会，在redis新增键值对（键：getPageArticleList，
//								       值：序列化后的PageInfo<Article>）
//	2.在缓存区间cacheArticle会添加值为新增键值对的key的数据
	@Cacheable(value = "cacheArticle")
	public PageInfo<Article> getPageArticleList(Integer pageIndex, Integer pageSize) {
		PageHelper.startPage(pageIndex,pageSize);//获取第pageIndex页，pageSize条内容
		
		List<Article> articleList= articleMapper.findAll();
		//把每个文章对应的分类信息查出来
		for(Article a:articleList) {
			List<Category> categoryList= categoryMapper.listCategoryByArticleId(a.getArticleId());
			a.setCategoryList(categoryList);
		}
		
		return new PageInfo<Article>(articleList); //将articleList封装成PageInfo
	}

	//该注解会删除cacheArticle缓存区间的所有缓存以保证，新增之后查询会更新缓存来保证数据一致性
	@CacheEvict(value = "cacheArticle",allEntries=true)
	public void addArticle(Article article) {
		//添加文章信息到文章表
		articleMapper.addArticle(article);
		
		//添加文章和分类的对应信息(这里的ArticleId是从添加文章后返回的自增主键articleId获取的)
		for(Category c:article.getCategoryList()) {
			articleMapper.addArticleCategory(article.getArticleId(),c.getCategoryId());
		}
		//添加文章和标签的对应信息
		for(Tag t:article.getTagList()) {
			articleMapper.addArticleTag(article.getArticleId(),t.getTagId());
		}
		
	}
	
	@CacheEvict(value = "cacheArticle",allEntries=true)
	public void addArticleDraft(Article article) {
		articleMapper.addArticle(article);
	}
	
	//根据ID查找user,缓存中没有则查到的数据存到cacheArticle缓存区间
	//key为article_id，value为序列化后的Article对象
	@Cacheable(value = "cacheArticle",key="'article_'+#id")
	public Article getArticleById(Integer id) {
		return articleMapper.getArticleById(id);
	}

	@CacheEvict(value = "cacheArticle",allEntries = true)
	public void UpdateArticle(Article article) {
		articleMapper.UpdateArticle(article);
		for(Category c:article.getCategoryList()) {
			articleMapper.UPdateCategory(article.getArticleId(),c.getCategoryId());
		}
		
		for(Tag t:article.getTagList()) {
			articleMapper.UPdateTag(article.getArticleId(),t.getTagId());
		}
	}
	
	@CacheEvict(value = "cacheArticle",allEntries =true)
	public void deleteArticle(Integer id) {
		articleMapper.deleteArticle(id);
		articleMapper.deleteCategoryref(id);
		articleMapper.deleteTagref(id);
		
	}

	
}










